package com.feng.springboot01.dao;

import com.feng.springboot01.pojo.Department;
import com.feng.springboot01.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class EmployeeDao {
    @Autowired
    DepartmentDao departmentDao;
    //模拟数据库
    public static Map<Integer, Employee> employees;
    static {
        employees=new HashMap<>();
        employees.put(1,new Employee(1,"二狗下凡","33026@qq.com",0,new Department(0,"营销部")));
        employees.put(2,new Employee(2,"铁柱","33026@qq.com",0,new Department(1,"研发部")));
        employees.put(3,new Employee(3,"张三","33026@qq.com",0,new Department(2,"开发部")));
        employees.put(4,new Employee(4,"吴明","33026@qq.com",1,new Department(3,"研发部")));
    }

    //自增组件
    private static Integer initId=employees.size();
    //获得所有员工
    public Collection<Employee> getAllEmployee(){
        return  employees.values();
    }

    //通过id获取员工
    public Employee getById(int id){
        return  employees.get(id);
    }

    //添加员工
    public  void addEmployee(Employee employee){
        if (employee.getId()==null){
            employee.setId(++initId);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(initId++,employee);
    }
    //删除员工
    public  void  deleEmployee(int id){
        employees.remove(id);
    }
    //修改员工信息
    public void save(Employee employee){
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }
}
